package Thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

//自己模拟实现一个线程池；(固定线程池中线程的个数);
class MyThreadPoll{
    //让一个自带任务的阻塞队列充当该线程池的属性；
    private BlockingDeque<Runnable> queue=null;
    public MyThreadPoll(int n) throws InterruptedException {
        //设计该线程池的线程个数（固定个数）；
        queue = new LinkedBlockingDeque<>(1000);
        //创建N个线程；
        for(int i=0;i<n;i++) {
            Thread thread = new Thread(() -> {
                try {
                    while (true) {
                        Runnable take = queue.take();
                        take.run();
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });
            thread.start();
        }
    }
    //向队列中添加任务；
    public void submit(Runnable take) throws InterruptedException {
        queue.put(take);
    }
}
public class Demo35 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPoll myThreadPoll=new MyThreadPoll(1000);
        //向线程池中进行提交任务；
        for(int i=0;i<100;i++){
            int id=i;
            myThreadPoll.submit(()->{
                System.out.println("["+Thread.currentThread().getName()+"]"+"的"+"{id}="+id);
            });
        }
    }

}
